<template>
  <div class="app-container">
    <div style="margin-bottom: 10px">【{{$route.query.name}}】课程评论</div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评论时间" prop="evaTime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.evaTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择评论时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CompassVideoEvaList" @selection-change="handleSelectionChange">
      <el-table-column label="发布用户" align="center" prop="userName">
        <template #default="{row}">
          <el-link type="primary" :href="`#/user/detail?id=${row.uid}`">{{row.userName}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="评论时间" align="center" prop="evaTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.evaTime, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评论详情" align="center" prop="evaDesc"/>
      <el-table-column label="是否精选" align="center" prop="">
        <template #default="{row}">
          <span>{{row.isChosen ? '是' : '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="被赞数量" align="center" prop="likeNum"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:CompassVideoEva:edit']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateIsChoosen(scope.row)"
            v-hasPermi="['system:CompassVideoEva:edit']"
          >{{!scope.row.isChosen ? '设置精选' : '取消精选'}}
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:CompassVideoEva:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程评价详情表对话框 -->
    <el-dialog title="评价详情" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发布人" prop="videoId">
          <el-link type="primary" :href="`#/user/detail?id=${form.uid}`">{{form.userName}}</el-link>
        </el-form-item>
        <el-form-item label="评论详情" prop="evaDesc">
          <el-input v-model="form.evaDesc" placeholder="请输入评论详情" readonly/>
        </el-form-item>
        <el-form-item label="回复" prop="evaDesc">
          <el-input v-model="form.replayDesc" type="textarea" placeholder="回复"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm">保存</el-button>
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompassVideoEva,
  getCompassVideoEva,
  delCompassVideoEva,
  addCompassVideoEva,
  updateCompassVideoEva,
  exportCompassVideoEva,
  updateChosenFlag
} from '@/api/system/CompassVideoEva'

export default {
  name: 'CompassVideoEva',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程评价详情表表格数据
      CompassVideoEvaList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        videoId: this.$route.query.id,
        pageNum: 1,
        pageSize: 10,
        evaTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        videoId: [
          { required: true, message: '视频ID不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询课程评价详情表列表 */
    getList() {
      this.loading = true
      listCompassVideoEva(this.queryParams).then(response => {
        this.CompassVideoEvaList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    handleUpdateIsChoosen(row) {
      updateChosenFlag({
        id: row.id,
        flag: row.isChosen ? 0 : 1
      }).then(res => {
        row.isChosen = !row.isChosen
        this.msgSuccess('操作成功')
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        likeNum: null,
        evaDesc: null,
        videoId: null,
        isChosen: null,
        userName: null,
        evaTime: null,
        uId: null,
        updateTime: null,
        createTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加课程评价详情表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCompassVideoEva(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改课程评价详情表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCompassVideoEva(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addCompassVideoEva(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除课程评价详情表编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCompassVideoEva(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有课程评价详情表数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportCompassVideoEva(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>
